"""
##########################################
# Needed Librairies
##########################################
"""
from jhplot import P1D
from jhplot import HPlotJa
from jhplot.jadraw import JaKey

from java.lang import String
from java.io import File
from java.awt import Color
from java.awt import Font

from jscinttools.scint.emissionspectrum import ModEmSpec
from jscinttools.scint.emissionspectrum import CmInvToNm

from jscinttools.utilities.jheputilities import HPlotJaUtil


"""
##########################################
# Define is the plot is saved and/or displayed
##########################################
"""
showFigures = True
writeFigures = not showFigures

"""
##########################################
# Colors and Fonts
##########################################
"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)


"""
##########################################
# Files for Emission Spectra and QE
##########################################
"""
#emRT = P1D("RT", "/home/marc-antoine/Dropbox/BGO/LumSpectrum_BGO_RT_mod.txt")
#em5K = P1D("5K", "/home/marc-antoine/Dropbox/BGO/LumSpectrum_BGO_5K_mod.txt")
#pmQE = P1D("QE", "/home/marc-antoine/Dropbox/BGO/R6095_QE.txt", color=colors[2], style="p", symbol=8, errAll=False)

emRT = P1D("RT", "/home/marc-antoine/Documents/jCode/Dropbox/BGO/LumSpectrum_BGO_RT_mod.txt")
em5K = P1D("5K", "/home/marc-antoine/Documents/jCode/Dropbox/BGO/LumSpectrum_BGO_5K_mod.txt")
pmQE = P1D("QE", "/home/marc-antoine/Documents/jCode/Dropbox/BGO/R6095_QE.txt")

outPath = "/home/marc-antoine/Documents/jCode/Dropbox/BGO"


"""
##########################################
# Operation on the Emission Spectra
##########################################
"""

""" RoomT"""
emRT_nm = ModEmSpec.scaleEnergy(emRT, CmInvToNm())
emRT_nm_norm = ModEmSpec.normalizeAreaSpec(emRT_nm)
emRT_nm_norm_int = ModEmSpec.roundEnergyVal(emRT_nm_norm)
emRT_nm_norm_int_sh = ModEmSpec.removeMultipleVal(emRT_nm_norm_int)
emRT_nm_norm_int_sh_conv = ModEmSpec.convoluteQE(emRT_nm_norm_int_sh, pmQE)

emRT_nm_norm_scal = ModEmSpec.scaleSpec(emRT_nm_norm, 1000)
emRT_nm_norm_int_sh_conv_scal = ModEmSpec.scaleSpec(emRT_nm_norm_int_sh_conv, 100)

""" LowT"""
em5K_nm = ModEmSpec.scaleEnergy(em5K, CmInvToNm())
em5K_nm_norm = ModEmSpec.normalizeAreaSpec(em5K_nm)
em5K_nm_norm_int = ModEmSpec.roundEnergyVal(em5K_nm_norm)
em5K_nm_norm_int_sh = ModEmSpec.removeMultipleVal(em5K_nm_norm_int)
em5K_nm_norm_int_sh_conv = ModEmSpec.convoluteQE(em5K_nm_norm_int_sh, pmQE)

em5K_nm_norm_scal = ModEmSpec.scaleSpec(em5K_nm_norm, 1000)
em5K_nm_norm_int_sh_conv_scal = ModEmSpec.scaleSpec(em5K_nm_norm_int_sh_conv, 100)


"""
##########################################
# Graphical Properties for Emission Spectra
##########################################
"""
emRT_nm_norm_scal.setColor(colors[1])
emRT_nm_norm_scal.setStyle("l")
emRT_nm_norm_scal.setPenWidth(2)
emRT_nm_norm_scal.setSymbolSize(0)
emRT_nm_norm_scal.setErrAll(False)

emRT_nm_norm_int_sh_conv_scal.setColor(colors[1])
emRT_nm_norm_int_sh_conv_scal.setStyle("lp")
emRT_nm_norm_int_sh_conv_scal.setSymbol(6)
emRT_nm_norm_int_sh_conv_scal.setSymbolSize(6)
emRT_nm_norm_int_sh_conv_scal.setErrAll(False)

em5K_nm_norm_scal.setColor(colors[0])
em5K_nm_norm_scal.setStyle("l")
em5K_nm_norm_scal.setPenWidth(2)
em5K_nm_norm_scal.setSymbolSize(0)
em5K_nm_norm_scal.setErrAll(False)

em5K_nm_norm_int_sh_conv_scal.setColor(colors[0])
em5K_nm_norm_int_sh_conv_scal.setStyle("lp")
em5K_nm_norm_int_sh_conv_scal.setSymbol(4)
em5K_nm_norm_int_sh_conv_scal.setSymbolSize(6)
em5K_nm_norm_int_sh_conv_scal.setErrAll(False)

pmQE.setColor(colors[2])
pmQE.setStyle("l")
pmQE.setSymbolSize(0)
pmQE.setPenWidth(2)
pmQE.setErrAll(False)


"""
##########################################
# Keys for the Legend
##########################################
"""
emRTNormKey = JaKey(20, "RoomT Norm", 0.55, 0.20, "NDC")
emRTNormKey.setColor(emRT_nm_norm_scal.getColor())
emRTNormKey.setKeyColor(emRT_nm_norm_scal.getColor())
emRTNormKey.setPenWidth(2)
emRTNormKey.setFont(legendFont)

emRTNormConvKey = JaKey(emRT_nm_norm_int_sh_conv_scal.getSymbol(), "RoomT Conv", 0.55, 0.30, "NDC")
emRTNormConvKey.setColor(emRT_nm_norm_int_sh_conv_scal.getColor())
emRTNormConvKey.setKeyColor(emRT_nm_norm_int_sh_conv_scal.getColor())
emRTNormConvKey.setPenWidth(2)
emRTNormConvKey.setFont(legendFont)

em5KNormKey = JaKey(20, "5K Norm", 0.55, 0.25, "NDC")
em5KNormKey.setColor(em5K_nm_norm_scal.getColor())
em5KNormKey.setKeyColor(em5K_nm_norm_scal.getColor())
em5KNormKey.setPenWidth(2)
em5KNormKey.setFont(legendFont)

em5KNormConvKey = JaKey(em5K_nm_norm_int_sh_conv_scal.getSymbol(), "5K Conv", 0.55, 0.35, "NDC")
em5KNormConvKey.setColor(em5K_nm_norm_int_sh_conv_scal.getColor())
em5KNormConvKey.setKeyColor(em5K_nm_norm_int_sh_conv_scal.getColor())
em5KNormConvKey.setPenWidth(2)
em5KNormConvKey.setFont(legendFont)

qeKey = JaKey(20, "QE", 0.55, 0.40, "NDC")
qeKey.setColor(pmQE.getColor())
qeKey.setKeyColor(pmQE.getColor())
qeKey.setPenWidth(2)
qeKey.setFont(legendFont)


"""
##########################################
# Define the Plotter
##########################################
"""
mainPlotter = HPlotJa("Emission Spectra", 600, 600, 1, 1, True)
mainPlotter.setGTitle("BGO Emission Spectra", titleFont)
mainPlotter.setAntiAlias(False)
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
mainPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
mainPlotter.setRange(350,650,0,30)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.setTicFont(ticFont)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("Wavelength (nm)")
mainPlotter.setNameY("Intensity (a.u.) -- QE (%)")


"""
##########################################
# Plot the Curves
##########################################
"""
mainPlotter.draw(emRT_nm_norm_int_sh_conv_scal)
mainPlotter.draw(emRT_nm_norm_scal)
mainPlotter.draw(em5K_nm_norm_int_sh_conv_scal)
mainPlotter.draw(em5K_nm_norm_scal)
mainPlotter.draw(pmQE)

mainPlotter.draw(emRTNormKey)
mainPlotter.draw(emRTNormConvKey)
mainPlotter.draw(em5KNormKey)
mainPlotter.draw(em5KNormConvKey)
mainPlotter.draw(qeKey)

mainPlotter.update()


"""
##########################################
# Modify axes ans labels
##########################################
"""
mainPlotter.cd(1,1)
theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
theAxes_11.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_11)

labX = mainPlotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, mainPlotter, 0.025)
labY = mainPlotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, mainPlotter, -0.01)

mainPlotter.update()

"""
##########################################
# Save or not the Plot
##########################################
"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "BGO_Em_Spctra_Corr" + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()

corrFac = ModEmSpec.integrateSpec(emRT_nm_norm_int_sh_conv) / ModEmSpec.integrateSpec(em5K_nm_norm_int_sh_conv)
print("Integral Ratio RoomT/LowT = " + String.valueOf(corrFac))

print("That's all folks !")

